# -*-coding:utf-8 -*-
"""
专题报表-沪深股票-机构持股-机构持股明细表（按机构）
"""
from EmQuantAPI import *
import common.mainApi as mp
import common.handler_date_time as hd_dt
import common.handler_ctr_result as hd_cr
import common.dataFrame as df
import common.sql as db
import common.mainApi as ma
import config.base_info as bs

base_type = "ctr"
base_target = "target"
base_index = "InstHolderDetailInfo"

info_ = bs.INDEX_INFO.get(base_type).get(base_target).get(base_index)  # ->dict
param_code = info_.get("return_data")  # ->list
param_ = ",".join(param_code)
option_ = "OrgType=0,ReportDate="


def getParamOptionFromSql(tb_name_, page=1, num=100, reportdate="2020-12-31"):
    page_ = (page-1)*num
    sql_ = "SELECT SHAREHDNAME, CODE, REPORTDATE, STR_BAOGAOQI \
    from {0} where REPORTDATE = '{1}' limit {2}, {3}     ".format(
        tb_name_, reportdate, page_, num)
    data_list = db.getdata_from_sql_tolist(sql_)
    return data_list


def mainGetInstHolderDetailInfo(reportdate="2020-12-31", holderName="华富中小板指数增强型证券投资基金", holderCode="320003", predictYear='2020'):

    data = c.ctr(base_index, param_, option_+reportdate+",HolderName=" +
                 holderName+",HolderCode="+holderCode+",PredictYear="+predictYear)

    return data


if __name__ == "__main__":
    tb_name = "ctr_instholderinfo"
    param_list = getParamOptionFromSql(
        tb_name_=tb_name, page=2, num=98, reportdate="2020-12-31")
    
    mp.login()
    for k in param_list:
        data = mainGetInstHolderDetailInfo(
            reportdate=k[2], holderName=k[0], holderCode=k[1], predictYear=k[3])
        hd_cr.ctr_result_handler(data_res=data, index_=base_index)

    mp.stop()
    db.dbcon_dispose()  # close-db
